import 'package:flutter/material.dart';
import 'package:address_picker/address_picker.dart';

class Add extends StatefulWidget {
  @override
  State<Add> createState() => _AddState();
}

class _AddState extends State<Add> {
  String _province;
  String _city;
  String _area;
  bool _switched = false;
  String _addressText = '请选择省市区';
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('新增收货地址'),
        elevation: 0,
      ),
      body: Container(
        padding: EdgeInsets.only(left: 10, right: 10),
        child: Column(
          children: [
            Row(
              children: [
                Container(
                  width: 80,
                  child: Text('姓名'),
                ),
                Expanded(
                  child: TextField(
                    decoration: InputDecoration(
                        hintText: '请输入收货人姓名',
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                            color: Color.fromRGBO(184, 184, 184, 1),
                            fontSize: 15)),
                  ),
                )
              ],
            ),
            Divider(
              color: Colors.grey,
              height: 0.5,
            ),
            Row(
              children: [
                Container(
                  width: 80,
                  child: Text('手机号码'),
                ),
                Expanded(
                  child: TextField(
                    decoration: InputDecoration(
                        hintText: '请输入收货人手机号码',
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                            color: Color.fromRGBO(184, 184, 184, 1),
                            fontSize: 15)),
                  ),
                )
              ],
            ),
            Divider(
              color: Colors.grey,
              height: 0.5,
            ),
            Container(
              margin: EdgeInsets.only(top: 13, bottom: 13),
              child: Row(
                children: [
                  Container(
                    width: 80,
                    child: Text('省、市、区'),
                  ),
                  Expanded(
                    child: GestureDetector(
                      onTap: () {
                        showModalBottomSheet(
                            context: context,
                            builder: (context) => Container(
                                  height: 300,
                                  child: Column(
                                    children: [
                                      Container(
                                        child: Row(
                                          mainAxisAlignment:
                                              MainAxisAlignment.spaceBetween,
                                          children: [
                                            TextButton(
                                                onPressed: () {
                                                  Navigator.pop(context);
                                                },
                                                child: Text('取消')),
                                            TextButton(
                                                onPressed: () {
                                                  print(
                                                      '${this._province}${this._city}${this._area}');
                                                  setState(() {
                                                    this._addressText =
                                                        this._province +
                                                            this._city +
                                                            this._area;
                                                  });
                                                  Navigator.pop(context);
                                                },
                                                child: Text('确认'))
                                          ],
                                        ),
                                      ),
                                      Container(
                                        height: 250.0,
                                        child: AddressPicker(
                                          style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 17),
                                          mode: AddressPickerMode
                                              .provinceCityAndDistrict,
                                          onSelectedAddressChanged: (address) {
                                            setState(() {
                                              this._province = address
                                                  .currentProvince.province;
                                              this._city =
                                                  address.currentCity.city;
                                              this._area =
                                                  address.currentDistrict.area;
                                            });
                                          },
                                        ),
                                      ),
                                    ],
                                  ),
                                ));
                      },
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceBetween,
                        children: [
                          Container(
                            child: Text(
                              this._addressText,
                              style: TextStyle(
                                  color: Color.fromRGBO(184, 184, 184, 1)),
                            ),
                          ),
                          Icon(
                            Icons.arrow_forward_ios_outlined,
                            color: Color.fromRGBO(184, 184, 184, 1),
                            size: 15,
                          )
                        ],
                      ),
                    ),
                  ),
                ],
              ),
            ),
            Divider(
              color: Colors.grey,
              height: 0.5,
            ),
            Row(
              children: [
                Container(
                  width: 80,
                  child: Text('详细地址'),
                ),
                Expanded(
                  child: TextField(
                    decoration: InputDecoration(
                        hintText: '请输入详细地址',
                        border: InputBorder.none,
                        hintStyle: TextStyle(
                            color: Color.fromRGBO(184, 184, 184, 1),
                            fontSize: 15)),
                  ),
                )
              ],
            ),
            Divider(
              color: Colors.grey,
              height: 0.5,
            ),
            Container(
              height: 50,
              child: Row(
                mainAxisAlignment: MainAxisAlignment.spaceBetween,
                children: [
                  Text('设置默认地址'),
                  Container(
                    child: Switch(
                        value: this._switched,
                        onChanged: (value) {
                          setState(() {
                            this._switched = value;
                          });
                        },
                        activeColor: Colors.white,
                        activeTrackColor: Colors.red),
                  )
                ],
              ),
            ),
            SizedBox(
              width: 300,
              height: 50,
              child: ElevatedButton(
                onPressed: () {},
                style: ElevatedButton.styleFrom(primary: Colors.black),
                child: Text(
                  '确定添加',
                  style: TextStyle(color: Colors.white, fontSize: 18),
                ),
              ),
            )
          ],
        ),
      ),
    );
  }
}
